import Mock from 'mockjs'

const Random = Mock.Random

// 生成标签
const generateTags = () => {
  const tags = ['Vue', 'React', 'Angular', 'JavaScript', 'TypeScript', 'Node.js', 'Python', 'Java', 'Go', 'Docker', 'Kubernetes']
  const count = Random.integer(1, 4)
  return Random.shuffle(tags).slice(0, count)
}

export default [
  // 获取博客列表
  {
    url: '/api/blogs',
    method: 'get',
    response: ({ query }) => {
      const { page = 1, pageSize = 10 } = query
      const list = Array(pageSize).fill(0).map(() => ({
        id: Random.id(),
        title: Random.title(5, 10),
        author: Random.cname(),
        createTime: Random.datetime(),
        summary: Random.paragraph(1, 3),
        views: Random.integer(100, 10000),
        agrees: Random.integer(10, 1000),
        comments: Random.integer(0, 100),
        tags: generateTags()
      }))
      
      return {
        code: 200,
        data: {
          total: 100,
          list
        },
        message: '获取博客列表成功'
      }
    }
  },

  // 获取高赞博客列表
  {
    url: '/api/blogs/high-agree',
    method: 'get',
    response: () => {
      const list = Array(5).fill(0).map(() => ({
        id: Random.id(),
        title: Random.title(5, 10),
        author: Random.cname(),
        createTime: Random.datetime(),
        agrees: Random.integer(1000, 5000),
        comments: Random.integer(100, 500)
      }))
      
      console.log('生成的高赞博客数据:', list) // 添加调试日志
      
      return {
        code: 200,
        data: list,
        message: '获取高赞博客列表成功'
      }
    }
  },

  // 获取博客详情
  {
    url: '/api/blog/:id',
    method: 'get',
    response: () => {
      return {
        code: 200,
        data: {
          id: Random.id(),
          title: Random.title(5, 10),
          content: Random.paragraph(10, 20),
          author: Random.cname(),
          createTime: Random.datetime(),
          views: Random.integer(100, 10000),
          agrees: Random.integer(10, 1000),
          comments: Random.integer(0, 100),
          tags: generateTags()
        },
        message: '获取博客详情成功'
      }
    }
  },
  // 发布博客
  {
    url: '/api/blog/publish',
    method: 'post',
    response: ({ body }) => {
      // 验证必要字段
      const { title, content, category } = body
      if (!title || !content || !category) {
        return {
          code: 400,
          message: '缺少必要字段'
        }
      }

      return {
        code: 200,
        data: {
          id: Random.id(),
          ...body,
          author: Random.cname(),
          createTime: Random.datetime(),
          views: 0,
          agrees: 0,
          comments: 0
        },
        message: '发布成功'
      }
    }
  },

  // 保存草稿
  {
    url: '/api/blog/draft',
    method: 'post',
    response: ({ body }) => {
      return {
        code: 200,
        data: {
          id: Random.id(),
          ...body,
          createTime: Random.datetime(),
          status: 'draft'
        },
        message: '保存草稿成功'
      }
    }
  }
] 